package com.alisveris.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.alisveris.pojo.UrunResimPojo;
import com.dbConnector.DataBaseConnector;

public class PagingController extends DataBaseConnector{

	
	    /**
	 * ERKAN YILDIRIM
	 */
	private static final long serialVersionUID = 1L;
     
	  
	    private int maxEntriesPerPage;   
	    
        private int iStartResultNo=0;
        
	    private int iEndResultNo=0;
	    
	    private int pageNumber;
	    
	   
	    private ResultSet countPageResultSet;
	    
	    private ResultSet maxPageResultSet;
	    
	    
	    private int showRows; 
	    
	    private int totalSearchRecords; 
	    
	    private int offset=9;
	    
	    static{
	    	
	    }
	    
	    
	        
	    public List<UrunResimPojo> getUrunList(int pageNumber) throws Exception{
			
			List<UrunResimPojo> urunInfo = new ArrayList<UrunResimPojo>();
					
			UrunResimPojo urunResimPojo = null;
	   	    
			
		    if(pageNumber==1){
		    	maxEntriesPerPage=0;
		    }else if(pageNumber!=0){
		    	maxEntriesPerPage=getMaxEntriesPerPage()+getOffset();
		    }else{
		    	maxEntriesPerPage=0;
		    }
			
			
				
		 	String selectLimitSql = "select * from product_image i, product p where p.PRODUCT_IMAGE_ID=i.id order by i.id asc limit "+(getMaxEntriesPerPage())+","+getOffset();
		 	
		 	
		 	String selectTotalCountSql="select count(*) as cnt from  product_image i, product p where p.PRODUCT_IMAGE_ID=i.id;";
		 			 	
	    	PreparedStatement	maxPagePreparedStatement=null;
	    	
	    	PreparedStatement	countPagePreparedStatement=null;
	    	
	    	
	    	try {
				getDBConnection();
				
				maxPagePreparedStatement = getDbConnection().prepareStatement(selectLimitSql);
	     			
				countPagePreparedStatement=getDbConnection().prepareStatement(selectTotalCountSql);
				
				maxPageResultSet = maxPagePreparedStatement.executeQuery();				
				
			    countPageResultSet = countPagePreparedStatement.executeQuery();
							
	    
			    while(countPageResultSet.next()){	
			    	totalSearchRecords= Integer.parseInt(countPageResultSet.getString("cnt")); 	
			    }
			    	
		    	showRows=(int)(Math.ceil((double)totalSearchRecords/getOffset()));		    	    	
			    
				while(maxPageResultSet.next()){
				
						
					
					
					
					urunResimPojo= new UrunResimPojo();
					
					
					urunResimPojo.setImageUrl(maxPageResultSet.getString("IMAGE_URL"));
					urunResimPojo.setKod(maxPageResultSet.getString("KOD"));
					urunResimPojo.setIsmi(maxPageResultSet.getString("ISMI"));
					
					if(maxPageResultSet.getString("MIKTAR")!=null){				 
						int miktar =  Integer.parseInt(maxPageResultSet.getString("MIKTAR"));					
						urunResimPojo.setMiktar(miktar);				
					}
					
					if(maxPageResultSet.getString("TALEP_MIKTAR")!=null){				 
						int talepMiktari =  Integer.parseInt(maxPageResultSet.getString("TALEP_MIKTAR"));					
						urunResimPojo.setTalepMiktar(talepMiktari);				
					}
					
					if(maxPageResultSet.getString("FIYAT")!=null){				   
						float fiyat  = Float.parseFloat(maxPageResultSet.getString("FIYAT"));
						urunResimPojo.setFiyat(fiyat);					
					}
										 
					urunInfo.add(urunResimPojo);
								
				}
				
				
			} catch (SQLException e) {
				e.printStackTrace();
	 
			} finally {
	 
				if (maxPagePreparedStatement != null) {
					maxPagePreparedStatement.close();
				}
				
				if (countPagePreparedStatement != null) {
					countPagePreparedStatement.close();
				}
				
	 
				if (getDbConnection() != null) {
					getDbConnection().close();
				}	 
			}
					
			
		  return urunInfo;	
		}
	      

		public void setCountPageResultSet(ResultSet countPageResultSet) {
			this.countPageResultSet = countPageResultSet;
		}



		public ResultSet getCountPageResultSet() {
			return countPageResultSet;
		}



		public void setMaxPageResultSet(ResultSet maxPageResultSet) {
			this.maxPageResultSet = maxPageResultSet;
		}



		public ResultSet getMaxPageResultSet() {
			return maxPageResultSet;
		}









		public void setiStartResultNo(int iStartResultNo) {
			this.iStartResultNo = iStartResultNo;
		}



		public int getiStartResultNo() {
			return iStartResultNo;
		}



		public void setiEndResultNo(int iEndResultNo) {
			this.iEndResultNo = iEndResultNo;
		}



		public int getiEndResultNo() {
			return iEndResultNo;
		}



		public void setMaxEntriesPerPage(int maxEntriesPerPage) {
			this.maxEntriesPerPage = maxEntriesPerPage;
		}



		public int getMaxEntriesPerPage() {
			return maxEntriesPerPage;
		}



		public void setShowRows(int showRows) {
			this.showRows = showRows;
		}



		public int getShowRows() {
			return showRows;
		}



		public void setPageNumber(int pageNumber) {
			this.pageNumber = pageNumber;
		}



		public int getPageNumber() {
			return pageNumber;
		}



		public void setTotalSearchRecords(int totalSearchRecords) {
			this.totalSearchRecords = totalSearchRecords;
		}



		public int getTotalSearchRecords() {
			return totalSearchRecords;
		}



		public void setOffset(int offset) {
			this.offset = offset;
		}



		public int getOffset() {
			return offset;
		}


	
}
